const express = require('express');
const router = express.Router();
const iconvLite = require('iconv-lite');

const multer = require('multer');
const { create, detail, configDuty, getList, importTxt } = require('../../service/user/roleService');

const upload = multer({
  storage: multer.diskStorage({
    destination: (req, file, cb) => {
      cb(null, './uploads');
    },
    filename: (req, file, cb) => {
      const { fieldname, originalname } = file;
      file.originalname = iconvLite.decode(originalname, 'gbk');
      const after = originalname.split('.')[1] ? `.${originalname.split('.')[1]}` : '.txt';
      cb(null, fieldname + after);
    },
  }),
});

router.post('/createRole', create);
router.get('/roleList', getList);
router.post('/configDuty', configDuty);
router.get('/detail', detail);
router.post('/import', upload.single('file'), importTxt);

module.exports = router;
